//--- The programme "NonSinusoidalDrivenDampedOscillator", Ebrahim Foulaadvand, 08 August 2012 ----- #include #include #include #include #include #include #include #include #include #include #include using namespace std; double a (double &t,double &x,double &v); // a is the accelration function double tau=0.01,T=100,ac,k=9.,m=1.,w0=sqrt(k/m),x0=0.,v0=0.,gamma=0.5,beta=gamma/(2*m),omega=2., period=2*M_PI/omega,delta,A0=1.,E,kx1,kx2,lx1,lx2,tmid,xmid,vmid,Anumeric=0.; // x0=initial position ; v0=initial velocity ; tau=timestep; T=simulation time, // gamma=damping coefficient; omega= frequency of external force int N=(T/tau), main() { vector v(N+1,0),x(N+1,0); ofstream xER("xER nonsinusoidal omega=2.0.plt"); ofstream xRK2("xRK2 nonsinusoidal.plt"); ofstream aER("aER nonsinusoidal.plt"); //------------------------ Euler-Richardson Method------------------------ x[0]=x0; v[0]=v0; Anumeric=0; for(int t=0;t(10/beta) && t*tau<(10/beta) + 2*2*(M_PI/omega) && x[t]>0 && x[t+1]>x[t]) Anumeric=x[t+1]; }//end t //cout<<"Anumeric ER= "<(10/beta) && t*tau<(10/beta) + 2*(M_PI/omega) && x[t]>0 && x[t+1]>x[t]) Anumeric=x[t+1]; }//end t //cout<<"Anumeric RK2= "<=0.75*period ) { return ac=-k*x/m-(gamma/m)*v + A0*cos(omega*t); } if ( fmod(t,period)>0.25*period && fmod(t,period)<0.75*period ) { return ac=-k*x/m-(gamma/m)*v; } } /* double a (double &t,double &x,double &v) { return ac=-k*x/m-(gamma/m)*v + A0*cos(omega*t); } */